package com.ruoyi.classai.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.classai.domain.EwcChapterTeachingResources;
import com.ruoyi.classai.domain.EwcTeachingResource;
import com.ruoyi.classai.domain.dto.resource.EwcTeachingResourceDto;
import com.ruoyi.classai.domain.dto.resource.EwcTeachingResourceSaveDto;
import com.ruoyi.classai.domain.vo.resource.EwcTeachingResourceAnalyseDetailVo;
import com.ruoyi.classai.domain.vo.resource.EwcTeachingResourceVo;
import com.ruoyi.classai.mapper.EwcTeachingResourceMapper;
import com.ruoyi.classai.service.IEwcTeachingResourceService;
import com.ruoyi.common.core.context.ThirdSessionHolder;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 教学视频解析对象Service业务层处理
 *
 * @author ZSP
 * @date 2025-06-07
 */
@Service
public class EwcTeachingResourceServiceImpl extends ServiceImpl<EwcTeachingResourceMapper, EwcTeachingResource> implements IEwcTeachingResourceService {

    @Autowired
    private EwcTeachingResourceMapper ewcTeachingResourceMapper;

    @Override
    public String addResource(EwcTeachingResourceDto ewcTeachingResourceDto) {
        String resourceId = "";
        EwcTeachingResource teachingResource = new EwcTeachingResource();
        String teachResourceFlowId = UUID.randomUUID().toString().replace("-", "");
        Date currentDate = new Date();
        teachingResource.setResourceName(ewcTeachingResourceDto.getResourceName());
        teachingResource.setResourceUrl(ewcTeachingResourceDto.getResourceUrl());
        teachingResource.setSubjectType(ewcTeachingResourceDto.getSubjectType());
        teachingResource.setStatus(1);
        teachingResource.setTaskId(ewcTeachingResourceDto.getTaskId());
        teachingResource.setUpdateTime(currentDate);
        teachingResource.setRemark(ewcTeachingResourceDto.getRemark());
        if(ewcTeachingResourceDto.getId() == null || ewcTeachingResourceDto.getId().isEmpty()){
            teachingResource.setCreatorId(ThirdSessionHolder.getThirdSession().getId());
            teachingResource.setCreator(ThirdSessionHolder.getThirdSession().getTeacherName());
            teachingResource.setId(teachResourceFlowId);
            teachingResource.setCreateTime(currentDate);
            boolean result = this.save(teachingResource);
            if(result){
                resourceId = teachResourceFlowId;
            }
            return resourceId;
        }
        UpdateWrapper<EwcTeachingResource> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", ewcTeachingResourceDto.getId())
                .eq("creator_id", ThirdSessionHolder.getThirdSession().getId());
        boolean resultUp =  this.update(teachingResource,updateWrapper);
        if(resultUp){
            resourceId = ewcTeachingResourceDto.getId();
        }
        return resourceId;
    }

    @Override
    public List<EwcTeachingResourceVo> getTeachingResourceList(String resourceName, Integer subjectType, String deadlineStart, String deadlineEnd) {
        return ewcTeachingResourceMapper.getTeachingResourceList(ThirdSessionHolder.getThirdSession().getId(),resourceName,subjectType,deadlineStart,deadlineEnd);
    }

    @Override
    public boolean deleteResource(String id) {
        UpdateWrapper<EwcTeachingResource> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id)
                     .eq("creator_id", ThirdSessionHolder.getThirdSession().getId());
        EwcTeachingResource entity = new EwcTeachingResource();
        entity.setStatus(0);
        return this.update(entity,updateWrapper);
    }

    @Override
    public EwcTeachingResourceAnalyseDetailVo getTeachingResourceAnalyseDetail(String id) {
        return ewcTeachingResourceMapper.getTeachingResourceAnalyseDetail(id,ThirdSessionHolder.getThirdSession().getId());
    }

}
